package com.mmy.controller.ship;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.controller.Common;
import com.mmy.controller.UtilConstants;
import com.mmy.domain.ship.TShipBase;
import com.mmy.domain.ship.TShipOrder;
import com.mmy.domain.ship.TShipOrg;
import com.mmy.domain.ship.VShipBase;
import com.mmy.domain.ship.VShipOrder;
import com.mmy.domain.ship.VShipPlan;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.ship.impl.TShipBaseServiceImpl;
import com.mmy.service.ship.impl.TShipOrderServiceImpl;
import com.mmy.service.ship.impl.TShipOrgServiceImpl;
import com.mmy.service.ship.impl.TShipPlanServiceImpl;

@Controller
public class ShipBaseController {

	@Autowired
	TShipBaseServiceImpl tShipBaseServiceImpl;
	@Autowired
	TShipOrgServiceImpl tShipOrgServiceImpl;
	@Autowired
	TShipOrderServiceImpl tShipOrderServiceImpl;
	@Autowired
	TShipPlanServiceImpl tShipPlanServiceImpl;
	@Autowired
	Common common;

	/**
	 * 函数功能说明： 跳转
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/shipbase")
	public String ToShipBase(Model model) {
		try {

			List<VShipBase> lstShipBase = tShipBaseServiceImpl.findAll();
			model.addAttribute("shipbaselist", lstShipBase);

			return "webbackground/ship/shipbase";
		} catch (Exception e) {

		}
		return null;

	}

	@RequestMapping(value = "/createshipbase")
	public String ToCreateShipBase(Model model) {

		List<TShipOrg> lstShipOrg = tShipOrgServiceImpl.findAll();
		model.addAttribute("shiporglist", lstShipOrg);

		return "webbackground/ship/createshipbase";
	}

	@RequestMapping(value = "/shipbaseinfo")
	public String ToShipBaseInfo(HttpServletRequest request, Model model) {

		int iShipID = Integer.parseInt(request.getParameter("iShipID"));
		model.addAttribute("iShipID", iShipID);
		VShipBase vShipBase = tShipBaseServiceImpl.findByID(iShipID);
		model.addAttribute("vShipBase", vShipBase);

		List<VShipOrder> lstShipOrder = tShipOrderServiceImpl.findByShipID(iShipID);
		model.addAttribute("orderlist", lstShipOrder);

		return "webbackground/ship/shipbaseinfo";
	}

	@RequestMapping(value = "/ordershipinfo")
	public String ToOrderShipInfo(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TShipOrder tShipOrder = tShipOrderServiceImpl.findByID(iID);
		model.addAttribute("tShipOrder", tShipOrder);

		String strOrderID = tShipOrder.getOrderID();
		int iPlanID = Integer.parseInt(strOrderID.substring(6, 12));

		VShipPlan vShipPlan = tShipPlanServiceImpl.findByID(iPlanID);

		model.addAttribute("vShipPlan", vShipPlan);

		return "webbackground/ship/ordershipinfo";
	}

//收款打印页面
	@RequestMapping(value = "/printship")
	public String ToPrintShip(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TShipOrder tShipOrder = tShipOrderServiceImpl.findByID(iID);
		model.addAttribute("tShipOrder", tShipOrder);

		return "webbackground/ship/printship";
	}

	// 退款打印页面
	@RequestMapping(value = "/printcancelship")
	public String ToPrintCancelShip(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TShipOrder tShipOrder = tShipOrderServiceImpl.findByID(iID);
		model.addAttribute("tShipOrder", tShipOrder);

		return "webbackground/ship/printcancelship";
	}

	/**
	 * 函数功能说明： 出纳审核
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/cashiership")
	public String ToCashierShip(Model model) {
		try {

			List<VShipBase> lstShipBase = tShipBaseServiceImpl.findAll();
			model.addAttribute("slist", lstShipBase);

			return "OA/cashier/cashiership";
		} catch (Exception e) {

		}
		return null;

	}

	@RequestMapping(value = "/cashiershiporder")
	public String ToCashierShipOrder(HttpServletRequest request, Model model) {

		int iShipID = Integer.parseInt(request.getParameter("iShipID"));

		List<VShipOrder> lstShipOrder = tShipOrderServiceImpl.findByShipID(iShipID);
		model.addAttribute("orderlist", lstShipOrder);

		model.addAttribute("iShipID", iShipID);

		return "OA/cashier/cashiershiporder";
	}

	@RequestMapping(value = "/entercheckcashiership")
	public String EnterCheckCashierShip(HttpServletRequest request, HttpSession session, String checkList) {

		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");

		String[] checklist = checkList.split(",");
		List<Integer> lstCashierID = new ArrayList<>();
		List<TShipOrder> lstCashier = new ArrayList<>();

		for (String str : checklist) {
			lstCashierID.add(Integer.parseInt(str));
			int i = Integer.parseInt(str);
			TShipOrder tShipOrder = tShipOrderServiceImpl.findByID(i);

			if (tShipOrder.getCashierState() == 0) {
				tShipOrder.setCashierState((byte) 1);
			} else if (tShipOrder.getCashierState() == 1) {
				tShipOrder.setCashierState((byte) 2);
			} else {
				tShipOrder.setCashierState((byte) 1);
			}
			tShipOrder.setCashierDate(new Date());
			tShipOrder.setCashierID(tSysUser.getUserID());

			lstCashier.add(tShipOrder);
		}

		tShipOrderServiceImpl.saveAll(lstCashier);

		return "OA/cashier/cashiershiporder";

	}

//查询
	@RequestMapping(value = "/searchcashiership")
	public String FindCashierShipOrder(HttpServletRequest request, Model model) {

		int iShipID = Integer.parseInt(request.getParameter("iShipID"));
		String strCashierState = request.getParameter("CashierState");
		if (strCashierState == "") {

			List<VShipOrder> lstShipOrder = tShipOrderServiceImpl.findByShipID(iShipID);
			model.addAttribute("orderlist", lstShipOrder);
			model.addAttribute("iShipID", iShipID);
			return "OA/cashier/cashiershiporder";
		} else {
			List<VShipOrder> lstShipOrder = tShipOrderServiceImpl.findByCashierAndShipID(strCashierState, iShipID);
			model.addAttribute("orderlist", lstShipOrder);
			model.addAttribute("iShipID", iShipID);
			return "OA/cashier/cashiershiporder";
		}

	}

	/**
	 * 函数功能说明：会计对账
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/accountship")
	public String ToAccountShip(HttpServletRequest request, Model model) {

		List<VShipBase> lstShipBase = tShipBaseServiceImpl.findAll();
		model.addAttribute("slist", lstShipBase);

		return "OA/account/accountship";
	}

	@RequestMapping(value = "/accountshiporder")
	public String ToAccountShipOrder(HttpServletRequest request, Model model) {

		int iShipID = Integer.parseInt(request.getParameter("iShipID"));

		List<VShipOrder> lstShipOrder = tShipOrderServiceImpl.findByShipID(iShipID);
		model.addAttribute("orderlist", lstShipOrder);

		model.addAttribute("iShipID", iShipID);

		return "OA/account/accountshiporder";
	}

	@RequestMapping(value = "/entercheckaccountship")
	public String EnterCheckAccountShip(HttpServletRequest request, String checkList, HttpSession session) {

		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");

		String[] checklist = checkList.split(",");
		List<Integer> lstAccountID = new ArrayList<>();
		List<TShipOrder> lstAccount = new ArrayList<>();

		for (String str : checklist) {
			lstAccountID.add(Integer.parseInt(str));
			int i = Integer.parseInt(str);
			TShipOrder tShipOrder = tShipOrderServiceImpl.findByID(i);

			if (tShipOrder.getAccountState() == 0) {
				tShipOrder.setAccountState((byte) 1);
			} else if (tShipOrder.getAccountState() == 1) {
				tShipOrder.setAccountState((byte) 2);
			} else {
				tShipOrder.setAccountState((byte) 1);
			}
			tShipOrder.setAccountDate(new Date());
			tShipOrder.setAccountID(tSysUser.getUserID());

			lstAccount.add(tShipOrder);
		}

		tShipOrderServiceImpl.saveAll(lstAccount);

		return "OA/account/accountshiporder";

	}

//	结账汇款
	@RequestMapping(value = "/entercheckremittanceship")
	public String EnterCheckRemittanceShip(HttpServletRequest request, String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstRemittanceID = new ArrayList<>();
		List<TShipOrder> lstRemittance = new ArrayList<>();

		for (String str : checklist) {
			lstRemittanceID.add(Integer.parseInt(str));
			int i = Integer.parseInt(str);
			TShipOrder tShipOrder = tShipOrderServiceImpl.findByID(i);

			if (tShipOrder.getRemittanceState() == 0) {
				tShipOrder.setRemittanceState((byte) 1);
			} else {
				tShipOrder.setRemittanceState((byte) 0);
			}

			lstRemittance.add(tShipOrder);
		}

		tShipOrderServiceImpl.saveAll(lstRemittance);

		return "OA/account/accountshiporder";

	}

//查询
	@RequestMapping(value = "/searchaccountship")
	public String FindAccountShip(HttpServletRequest request, Model model) {

		int iShipID = Integer.parseInt(request.getParameter("iShipID"));
		String strAccountState = request.getParameter("AccountState");

		if (strAccountState == "") {
			List<VShipOrder> lstShipOrder = tShipOrderServiceImpl.findByShipID(iShipID);
			model.addAttribute("orderlist", lstShipOrder);
			model.addAttribute("iShipID", iShipID);

			return "OA/account/accountshiporder";
		} else {
			List<VShipOrder> lstShipOrder = tShipOrderServiceImpl.findByAccountAndShipID(strAccountState, iShipID);
			model.addAttribute("orderlist", lstShipOrder);
			model.addAttribute("iShipID", iShipID);

			return "OA/account/accountshiporder";
		}

	}

	/**
	 * 函数功能说明： 新建游轮信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/subcreateshipbase")
	public String CreateShipBase(TShipBase tShipBase, HttpSession session) throws Exception {

		try {
			// 获取最大值
			tShipBase.setShipID(common.createBaseCode(UtilConstants.TableName.T_SHIP_BASE));
			tShipBase.setCreateDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			tShipBase.setCreator(tSysUser.getUserName());
			tShipBaseServiceImpl.save(tShipBase.getShipID());
			tShipBaseServiceImpl.save(tShipBase);
			return "redirect:shipbase";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除游轮信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@PostMapping("/delshipbase")
	@ResponseBody
	public String DelShipBase(String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstShipID = new ArrayList<>();

		for (String str : checklist) {
			lstShipID.add(Integer.parseInt(str));
		}

		tShipBaseServiceImpl.delShipBase(lstShipID);

		return "webbackground/ship/shipbase";
	}

	/**
	 * 函数功能说明： 修改游轮信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterupdateshipbase")
	public String EnterUpdateShipBase(HttpServletRequest request, Model model) {

		int iShipID = Integer.parseInt(request.getParameter("iShipID"));

		TShipBase tShipBase = tShipBaseServiceImpl.findByShipID(iShipID);
		model.addAttribute("tShipBase", tShipBase);
		List<TShipOrg> lstShipOrg = tShipOrgServiceImpl.findAll();
		model.addAttribute("shiporglist", lstShipOrg);
		return "webbackground/ship/updateshipbase";

	}

	@RequestMapping(value = "/updateshipbase")
	public String UpdateShipBase(TShipBase tShipBase, HttpSession session) throws Exception {
		try {
//			 定义一个新的实体类对象 
			TShipBase dtShipBase = tShipBaseServiceImpl.findByShipID(tShipBase.getShipID());
			dtShipBase.setOrgID(tShipBase.getOrgID());
			dtShipBase.setShipName(tShipBase.getShipName());
			dtShipBase.setMemo(tShipBase.getMemo());
			dtShipBase.setModifyDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtShipBase.setReviser(tSysUser.getUserName());
			tShipBaseServiceImpl.save(dtShipBase);
			return "redirect:shipbase";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 查询游轮信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/searchshipbase")
	public String FindShipBase(HttpServletRequest request, Model model) {

		String strShipName = request.getParameter("ShipName");
		if (strShipName == "") {
			List<VShipBase> lstShipBase = tShipBaseServiceImpl.findAll();
			model.addAttribute("shipbaselist", lstShipBase);
			request.setAttribute("strShipName", strShipName);
			return "webbackground/ship/shipbase";
		} else {
			List<VShipBase> lstShipBase = tShipBaseServiceImpl.findByName(strShipName);
			model.addAttribute("shipbaselist", lstShipBase);
			request.setAttribute("strShipName", strShipName);
			return "webbackground/ship/shipbase";
		}

	}

}
